﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Workflow.Activities;

namespace Openfree.Workflow.Common
{
    public class WorkflowService : IExternalService
    {
        #region IExternalService Members
        public event EventHandler<ExternalEventArgs> Update;
        public event EventHandler<ExternalEventArgs> Approve;
        public event EventHandler<ExternalEventArgs> Reject;
        public event EventHandler<ExternalEventArgs> SendBack;
        #endregion

        public void RaiseUpdateEvent(Guid instanceID, Object externalData)
        {
            if (Update != null)
            {
                ExternalEventArgs e = new ExternalEventArgs(instanceID, externalData);
                e.WaitForIdle = true;
                Update(null, e);
            }
        }

        public void RaiseApproveEvent(Guid instanceID, Object externalData)
        {
            if (Approve != null)
            {
                ExternalEventArgs e = new ExternalEventArgs(instanceID, externalData);
                e.WaitForIdle = true;
                Approve(null, e);
            }
        }

        public void RaiseRejectEvent(Guid instanceID, Object externalData)
        {
            if (Reject != null)
            {
                ExternalEventArgs e = new ExternalEventArgs(instanceID, externalData);
                e.WaitForIdle = true;
                Reject(null, e);
            }
        }

        public void RaiseSendBackEvent(Guid instanceID, Object externalData)
        {
            if (SendBack != null)
            {
                ExternalEventArgs e = new ExternalEventArgs(instanceID, externalData);
                e.WaitForIdle = true;
                SendBack(null, e);
            }
        }

        public delegate void BeforeSetState(object message);
        public event BeforeSetState MessageReceived;
        public void OnSendMessage(object message)
        {
            //工作流回传的message应包含instanceID
            if (MessageReceived != null)
            {
                MessageReceived(message);
            }
        }
         
	}
}
